// /mock/api/userApplication.js
const Mock = require('mockjs')
const Qs = require('qs')
const Random = Mock.Random
let userApplication = [] // 用于存放用户数据的数组
Random.extend({
  constellation: function () {
    const constellations = [
      '个人用户',
      '企业用户'
    ]
    return this.pick(constellations)
  },
  status: function () {
    const status = [
      '已拒绝',
      '待审批',
      '已批准'
    ]
    return this.pick(status)
  }

})
for (let i = 0; i < 85; i++) { // 生成100个对象放到数组中
  let template = {
    'id': i,
    'name': Random.cname(), // 生成姓名
    'applyDate': Random.date(), // 生成一个随机日期,可加参数定义日期格式
    'examineDate': Random.date(),
    'idNumber': Random.id(), // 生成2到10个字符之间的字符串
    'type': Random.constellation(),
    'status': Random.status(),
    'company': '河北路和汇源食品贸易有限公司',
    'srcList': [
      'http://passport.banging.cn/Fs5NQ1tfO-5gMEXjxCHGUZl2D5_R',
      'http://passport.banging.cn/Fr8ljP6jvKxYKwS6nIuWs1IQBtOM',
      'http://passport.banging.cn/FtjeECQO5GBG-mCYnDW9SYUz65Ld',
      'http://passport.banging.cn/FjbPVFAEK9xiCfsz942p96GBw_kA'
    ]
  }
  userApplication.push(template)
};
const getAllUserApplication = (params) => {
  if (params.body && userApplication.length) {
    let pageInfo = JSON.parse(Qs.parse(params).body)
    let start = (pageInfo.current - 1) * pageInfo.size
    let end = pageInfo.current * pageInfo.size
    var total = userApplication.length
    var applyList = userApplication.slice(start, end)
  }
  return { applyList, total }
}
const getUnprocessedUserApplication = (params) => {
  var unprocessedUserApplication = userApplication.filter(e => e.status === '待审批')
  if (params.body && unprocessedUserApplication.length) {
    let pageInfo = JSON.parse(Qs.parse(params).body)
    let start = (pageInfo.current - 1) * pageInfo.size
    let end = pageInfo.current * pageInfo.size
    var total = unprocessedUserApplication.length
    var applyList = unprocessedUserApplication.slice(start, end)
  }
  return { applyList, total }
}
const searchByName = (params) => {
  if (params.body) {
    let obj = JSON.parse(Qs.parse(params).body)
    var res = userApplication.filter(e => e.name === obj.name)
    var total = res.length
  }
  return { res, total }
}
const getUserInfo = (params) => {
  if (params.body) {
    let obj = JSON.parse(Qs.parse(params).body)
    var res = userApplication.find(e => String(e.id) === String(obj.id))
  }
  return { res }
}
// 创建API的URL，让vue通过URL获取数据
Mock.mock('/api/getAllUserApplication', 'get', getAllUserApplication) // 获取所有用户申请列表
Mock.mock('/api/getUnprocessedUserApplication', 'get', getUnprocessedUserApplication) // 获取未处理用户申请列表
Mock.mock('/api/searchByName', 'get', searchByName) // 通过名字查询
Mock.mock('/api/getUserInfo', 'get', getUserInfo) // 获取用户申请信息
